package com.mongo.jdbc.core;

import java.lang.reflect.Type;

import org.bson.types.ObjectId;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

/**
 * 
 * @author:binggong
 * @createtime:2019年11月10日 下午8:53:20
 * @version:V.0.1
 * @tag: JSON 处理工具类
 *
 */
public class GsonTools {

	public static Gson getGson () {
		  GsonBuilder builder = new GsonBuilder();
		   builder.registerTypeAdapter(ObjectId.class, new JsonSerializer<ObjectId>() {

			public JsonElement serialize(ObjectId src, Type typeOfSrc, JsonSerializationContext context) {
				// TODO Auto-generated method stub
				
				return  new JsonPrimitive(src.toString());
			}


//	            public String deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
//	                return new Date(json.getAsJsonPrimitive().getAsLong());
//	            }
	        });

		   
	     return builder.create();
	}
	
}
